package com.qnw.CardGroupManagement.fragment;
import android.os.*;
import android.support.v4.app.*;
import android.view.*;
import android.widget.*;
import com.qnw.CardGroupManagement.*;
import com.ygo.feihua.*;
import com.ygo.feihua.util.*;
import com.ygo.feihua.BmobTable.*;
import android.database.sqlite.*;
import android.database.*;
import java.util.*;
import cn.bmob.v3.*;
import cn.bmob.v3.listener.*;
import cn.bmob.v3.exception.*;
import com.ygo.feihua.adapter.*;
import com.qnw.CardGroupManagement.adapter.*;

public class FragmentQueryList extends Fragment
{

	ListView query_list;
	int type;
	ProgressBar query_pro;
	TextView query_text;
	List<Card> lc;
	CardListAdapter cladp;
	List<Zhuti> lz;
	ZhutiListAdapter zladp;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View v=inflater.inflate(R.layout.fragment_query_list,null);
		query_list=(ListView) v.findViewById(R.id.query_list);
		query_pro=(ProgressBar) v.findViewById(R.id.query_pro);
		query_text=(TextView) v.findViewById(R.id.query_text);
		type=getArguments().getInt("type");
		query_list.setVisibility(View.GONE);
		query_pro.setVisibility(View.GONE);
		query_text.setVisibility(View.GONE);
		
		switch(type){
			case DialogUtils.QUERY_CARD:
				lc=new ArrayList<Card>();
				cladp=new CardListAdapter(getActivity(),lc);
			//	query_list.setAdapter(cladp);
				
				break;
			case DialogUtils.QUERY_DECK:
			//	zhutiQuery(name);
				
				break;
			case DialogUtils.QUERY_KADIAN:

				break;
			case DialogUtils.QUERY_ZHUTI:
lz=new ArrayList<Zhuti>();
				zladp=new ZhutiListAdapter(getActivity(),lz);
				query_list.setAdapter(zladp);
				
				break;
		}
		
		// TODO: Implement this method
		return v;
	}
	
	public void messageQuery(String name){
		query_list.setVisibility(View.GONE);
		query_pro.setVisibility(View.VISIBLE);
		query_text.setVisibility(View.VISIBLE);
		query_text.setText("查询中,请稍等");
		switch(type){
			case DialogUtils.QUERY_CARD:
				lc.clear();
				for(Card c: Gongju.cardQuery(name)){
					lc.add(c);
				}
				
				if(lc.size()==0){
					query_list.setVisibility(View.GONE);
					query_pro.setVisibility(View.GONE);
					query_text.setVisibility(View.VISIBLE);
					query_text.setText("未查询到结果");
				}else{
					query_list.setVisibility(View.VISIBLE);
					query_pro.setVisibility(View.GONE);
					query_text.setVisibility(View.GONE);
				}
				cladp.notifyDataSetChanged();
				break;
			case DialogUtils.QUERY_DECK:
				
				break;
			case DialogUtils.QUERY_KADIAN:

				break;
			case DialogUtils.QUERY_ZHUTI:
				zhutiQuery(name);
				break;
		}
		
		
		
	}
	
	
	private void zhutiQuery(String name){
		BmobQuery<Zhuti> zt=new BmobQuery<Zhuti>();
		zt.addWhereEqualTo("ztname",name);
		zt.addWhereEqualTo("ztsh",1);	
		zt.addWhereEqualTo("vercode",Gongju.ZHUTI_VERCODE);
		zt.findObjects(new FindListener<Zhuti>(){

				@Override
				public void done(List<Zhuti> p1, BmobException p2)
				{
					if(p2==null){
						
						if(p1.size()==0){
							query_list.setVisibility(View.GONE);
							query_pro.setVisibility(View.GONE);
							query_text.setVisibility(View.VISIBLE);
							query_text.setText("未查询到结果");
						}else{
							lz.clear();
							for(Zhuti zzt:p1){
								lz.add(zzt);
							}
							query_list.setVisibility(View.VISIBLE);
							query_pro.setVisibility(View.GONE);
							query_text.setVisibility(View.GONE);
						}
						zladp.notifyDataSetChanged();
					}else{
						
						query_list.setVisibility(View.GONE);
						query_pro.setVisibility(View.GONE);
						query_text.setVisibility(View.VISIBLE);
						query_text.setText("查询失败,原因为"+p2);//+Gongju.jsonBmobException(p2));
					}
					// TODO: Implement this method
				} 
			});
	}
	
	/*private Kadian kadianQuery(String name){
		
		
	}
	*/
	
	
}
